package Double_Point;

public class 和为s的数 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6};
        int target=5;
        //int[] sum=TwoSum(arr,target);
        int[] sum=TwoSum_Double(arr,target);
        for(int num:sum){
            System.out.print(" "+num);
        }
    }
    public static int[] TwoSum(int[] arr,int target){
        //暴力枚举
        for (int i = 0; i <arr.length-1 ; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if(arr[i] + arr[j]==target){
                    return new int[]{arr[i],arr[j]};
                }
            }
        }
        return new int[]{-1};
    }

    public static int[] TwoSum_Double(int[] arr,int target){
        //双指针算法
        int left=0;
        int right=arr.length-1;
        while (right>left){
            if (arr[right] + arr[left] > target) {
                right--;
            }else if(arr[right] + arr[left] < target){
                left++;
            }else {
                return new int[]{arr[left],arr[right]};
            }
        }
        return new int[]{-1};
    }
}
